// pages/patient/condition-records/records.js

const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
const app = getApp();

var patientId = -1;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    theme:"维护记录",
    recordList: [
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.patientId){
      patientId = options.patientId;
    }
    if(options.name){
      this.setData({
        name: options.name
      });
    }else{
      this.setData({
        name: app.globalData.myName
      });
    }
    this.loadRecordList();
    this.setScrollHeight();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  loadRecordList: function(){
    let that = this;
    util.request(api.getMeasureList, {
      patientId: patientId
    }).then(function(res){
      console.log("getMeasureList: " + JSON.stringify(res));
      var data = res;
      var rlist = [];
      for(var i in data){
        var arra = data[i].addTime;
        var title = arra + ' ' + data[i].measureTime;
        var temp = {
          name: that.data.name,
          measureTime: data[i].measureTime,
          lienLength: data[i].lienLength,
          leftArmline: data[i].leftArmline,
          rightArmline: data[i].rightArmline,
          lienState: data[i].lienState,
          addTime: data[i].addTime,
          exposure: data[i].exposure,
          title: title
        };
        var strDetail = JSON.stringify(temp);
        rlist.push({
          name: that.data.name,
          measureTime: data[i].measureTime,
          lienLength: data[i].lienLength,
          leftArmline: data[i].leftArmline,
          rightArmline: data[i].rightArmline,
          lienState: data[i].lienState,
          addTime: data[i].addTime,
          exposure: data[i].exposure,
          title: title,
          strInfo: strDetail
        })
      }
      that.setData({
        recordList: rlist
      });
    });
  },

  // 设置scroll view的高度
  setScrollHeight: function(){
    let that = this;
    let windowHeight = 0;
    let screenHeight = 0;
    wx.getSystemInfo({
      success: function(res) {
        windowHeight = res.windowHeight;
        screenHeight = res.screenHeight;
        console.log("windowHeight = " + windowHeight);
        console.log("screenHeight = " + screenHeight);
        let componentWrap = wx.createSelectorQuery();
        componentWrap.select('#complication').boundingClientRect();
        let complicationHeight = 0;
        componentWrap.exec(function (res) {
          console.log("complication height = " + res[0].height);
          complicationHeight = res[0].height;
          var temp = windowHeight - complicationHeight;
          console.log("scroll height = " + temp );
          that.setData({
            scrollHeight: temp
          });
        });
      }
    });
  },
})